/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { VibratorStopMode, Vibrator } from '@hadss/react_native_vibrator';
import React, { useState } from 'react';
import { View, Text, StyleSheet, ScrollView, TouchableOpacity, ColorValue } from 'react-native';
const TAG = 'VibratorTest';
// 查询是否支持传入的振动
const handleSupportEffect = (): void => {
  Vibrator.isSupportEffect('haptic.notice.success')
    .then((state: boolean) => {
      console.log(TAG, `isSupportEffect : ${state}`);
    })
    .catch((err) => {
      console.log(TAG, `handleSupportEffect error : ${JSON.stringify(err)}`);
    });
};

// 按时间振动
const handleStarVibrateTime = (): void => {
  Vibrator.startVibration(
    {
      type: 'time',
      duration: 3000,
    },
    {
      id: 0,
      usage: 'ring',
    },
  )
    .then(() => {
      console.log(TAG, 'handleStarVibrateTime done');
    })
    .catch((err) => {
      console.log(TAG, `handleStarVibrateTime error : ${JSON.stringify(err)}`);
    });
};

// 按文件振动，文件在原生工程rawfile目录下
const handleStartVibrateFromFile = (): void => {
  Vibrator.startVibration(
    {
      type: 'file',
      fileName: 'vibrator/file.json',
    },
    {
      id: 0,
      usage: 'alarm',
    },
  )
    .then(() => {
      console.log(TAG, 'handleStartVibrateFromFile done');
    })
    .catch((err) => {
      console.log(TAG, `handleStartVibrateFromFile error : ${JSON.stringify(err)}`);
    });
};

// 按Preset振动
const handleStartVibratePreset = (): void => {
  Vibrator.startVibration(
    {
      type: 'preset',
      effectId: 'haptic.notice.success',
      intensity: 1,
      count: 15,
    },
    {
      id: 0,
      usage: 'notification',
    },
  )
    .then(() => {
      console.log(TAG, 'handleStartVibratePreset done');
    })
    .catch((err) => {
      console.log(TAG, `handleStartVibratePreset: ${JSON.stringify(err)}`);
    });
};

// 按照指定模式停止振动
const handleStopVibrationByMode = (): void => {
  Vibrator.stopVibration(VibratorStopMode.VIBRATOR_STOP_MODE_TIME)
    .then(() => {
      console.log(TAG, 'handleStopVibrationByMode done');
    })
    .catch((err) => {
      console.log(TAG, `handleStopVibrationByMode error : ${JSON.stringify(err)}`);
    });
};

// 停止所有的振动
const handleStopVibration = (): void => {
  Vibrator.stopVibration()
    .then(() => {
      console.log(TAG, 'handleStopVibration done');
    })
    .catch((err) => {
      console.log(TAG, `handleStopVibration error : ${JSON.stringify(err)}`);
    });
};

// 停止所有的振动
const handleStopVibrationSync = (): void => {
  Vibrator.stopVibrationSync();
};

const dataList = [
  {
    id: 0,
    text: '查询是否支持传入参数effectId',
    onPress: handleSupportEffect,
  },
  {
    id: 1,
    text: '根据VibrateTime触发马达振动',
    onPress: handleStarVibrateTime,
  },
  {
    id: 2,
    text: '根据VibratePreset触发马达振动',
    onPress: handleStartVibratePreset,
  },
  {
    id: 3,
    text: '根据VibrateFromFile触发马达振动',
    onPress: handleStartVibrateFromFile,
  },
  {
    id: 4,
    text: '按照指定模式停止(time)马达振动',
    onPress: handleStopVibrationByMode,
  },
  {
    id: 5,
    text: 'stopVibration停止所有马达振动',
    onPress: handleStopVibration,
  },
  {
    id: 6,
    text: 'stopVibrationSync停止所有马达振动',
    onPress: handleStopVibrationSync,
  },
];

const VibratorTest = (): JSX.Element => {
  const selectColor = (index: number): { bgColor: ColorValue; textColor: ColorValue } => {
    return {
      bgColor: activeIndex === index ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)',
      textColor: activeIndex === index ? 'white' : 'rgb(10, 89, 247)',
    };
  };
  const touchItem = (text: string, index: number, onPress: () => void): JSX.Element => {
    const itemPress = (): void => {
      onPress();
      setActiveIndex(index);
    };
    return (
      <TouchableOpacity
        key={index + '--' + text}
        style={[{ backgroundColor: selectColor(index).bgColor }, styles.button]}
        onPress={itemPress}>
        <Text style={[{ color: selectColor(index).textColor }, styles.btnText]}>{text}</Text>
      </TouchableOpacity>
    );
  };
  const [activeIndex, setActiveIndex] = useState(0);
  return (
    <View style={styles.container}>
      <View style={styles.title}>
        <Text style={styles.titleText}>Vibrator Test</Text>
      </View>

      <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
        {dataList.map((item) => {
          return touchItem(item.text, item.id, item.onPress);
        })}
      </ScrollView>
    </View>
  );
};

export default VibratorTest;

const styles = StyleSheet.create({
  title: {
    flexDirection: 'row',
    justifyContent: 'flex-start',
    width: '100%',
    marginBottom: 16,
  },
  titleText: {
    fontSize: 30,
  },
  container: {
    backgroundColor: '#F1F3F5',
    paddingHorizontal: 16,
    paddingTop: 72,
    justifyContent: 'flex-start',
    alignItems: 'center',
    width: '100%',
    height: '100%',
  },
  scrollView: {
    width: '100%',
  },
  geometryTransitionView: {
    marginVertical: 10,
    height: 200,
    width: 300,
  },
  textinput: {
    backgroundColor: 'white',
    width: '100%',
    minHeight: 120,
    marginBottom: 48,
    borderRadius: 16,
    paddingHorizontal: 16,
    paddingVertical: 8,
    color: 'rgba(0, 0, 0, 0.6)',
  },
  button: {
    width: '100%',
    height: 40,
    borderRadius: 20,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 12,
  },
  btnText: {
    fontSize: 16,
    fontWeight: '500',
  },
});
